package com.hopes.bizz.risk.transfer.validation;

import com.hopes.bizz.common.core.util.CollectionUtils;
import com.hopes.bizz.common.core.util.StringUtils;
import com.hopes.bizz.risk.transfer.constant.RiskConstant;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.List;

/**
 * @author lyb
 * @version 1.0
 * @date 2024/2/1 14:43
 */
public class MeasuresClassifyValidator implements ConstraintValidator<MeasuresClassify, String> {

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isBlank(value)) {
            return false;
        }
        String[] classifyArr = value.split(RiskConstant.VALIDATOR_JOIN);

        if (classifyArr.length != 2) {
            return true;
        }

        String classify1 = classifyArr[0];
        String classify2 = classifyArr[1];

        List<String> classify2List = RiskConstant.RISK_MEASURES_CLASSIFY_MAP.get(classify1);
        if (CollectionUtils.isEmpty(classify2List)) {
            return false;
        }
        return classify2List.contains(classify2);
    }
}
